package com.qfedu.aunt.commons.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qfedu.aunt.commons.aop.BehaviorLog;
import com.qfedu.aunt.commons.dto.AdminDTO;
import com.qfedu.aunt.commons.pojo.Admin;
import com.qfedu.aunt.commons.result.R;
import com.qfedu.aunt.commons.service.AdminService;
import com.qfedu.aunt.commons.vo.AdminAddVo;
import com.qfedu.aunt.commons.vo.AdminQueryVo;
import com.qfedu.aunt.commons.vo.AdminUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.InvocationTargetException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaobobo
 * @since 2022-10-31
 */
@RestController
@RequestMapping("/aunt/commons/admin")
@Api(description = "管理员接口",tags = "管理员接口")
public class AdminController {


    @Autowired
    private AdminService adminService;

    /**
     * 分页查询管理员数据
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页查询管理员数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码",dataType = "int",paramType = "path",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量大小",dataType = "int",paramType = "path",required = true),
            @ApiImplicitParam(name = "token",value = "用户身份标识",dataType = "string",paramType = "header",required = true)
    })
    @GetMapping("/findAdminByPage/{currentPage}/{pageSize}")
    @BehaviorLog(methodName = "分页查询菜单",modelName = "管理员模块",paramExists = true)
    public R  findAdminByPage(@PathVariable("currentPage") Integer currentPage,
                              @PathVariable("pageSize") Integer pageSize){
        IPage<AdminDTO> page=adminService.findAdminByPage(currentPage,pageSize);
        return R.ok().setData("adminList",page.getRecords()).setData("total",page.getTotal());

    }


    /**
     * 他通过id删除用户
     * @param adminId
     * @return
     */
    @GetMapping("/deleteById/{adminId}")
    @ApiOperation(value = "通过id删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId",value = "管理员id",dataType = "string",paramType = "path",required = true),
            @ApiImplicitParam(name = "token",value = "用户身份标识",dataType = "string",paramType = "header",required = true)
    })
    public R deleteById(@PathVariable("adminId") String adminId){
        adminService.deleteById(adminId);
        return R.ok();
    }

    /**
     * 批量删除数据
     * @param adminIds
     * @return
     */
    @GetMapping("/deleteByIds")
    @ApiOperation(value = "通过id删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminIds",value = "管理员ids",dataType = "string",paramType = "query",required = true),
            @ApiImplicitParam(name = "token",value = "用户身份标识",dataType = "string",paramType = "header",required = true)
    })
    public R deleteByIds(String adminIds){
        adminService.deleteByIds(adminIds);
        return R.ok();
    }

    /**
     * 组合条件进行查询找到Admin对象
      * @param adminQueryVo
     * @return
     */
    @PostMapping("/findAdminByCondition/{currentPage}/{pageSize}")
    @ApiOperation(value = "通过id删除用户")
    @ApiImplicitParam(name = "token",value = "用户身份标识",dataType = "string",paramType = "header",required = true)
    public R findAdminByCondition(@RequestBody AdminQueryVo adminQueryVo, @PathVariable("currentPage") Integer currentPage,@PathVariable("pageSize") Integer pageSize) throws InvocationTargetException, IllegalAccessException {
        IPage<AdminDTO> adminDTOIPage=adminService.findAdminByCondition(adminQueryVo,currentPage,pageSize);
        return R.ok().setData("adminList",adminDTOIPage.getRecords()).setData("total",adminDTOIPage.getTotal());
    }


    /**
     * 添加管理员
     * @param file
     * @param adminAddVo
     * @return
     */
    @PostMapping("/addAdmin")
    @ApiOperation(value = "添加管理员")
    @ApiImplicitParam(name = "token",value = "用户身份标识",dataType = "string",paramType = "header",required = true)
    public R addAdmin(MultipartFile file, AdminAddVo adminAddVo) throws InvocationTargetException, IllegalAccessException {
         adminService.addAdmin(file,adminAddVo);
        return R.ok();
    }





    @PostMapping("/updateAdmin")
    @ApiOperation(value = "更新管理员")
    @ApiImplicitParam(name = "token",value = "用户身份标识",dataType = "string",paramType = "header",required = true)
    @BehaviorLog(modelName = "管理员模块",methodName = "更新管理员",paramExists = true)
    public R updateAdmin(@RequestBody AdminUpdateVo adminUpdateVo) throws Exception {
        adminService.updateAdmin(adminUpdateVo);
        return R.ok();
    }




}

